package com.asiainfo.hybridapp.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.asiainfo.hybridapp.R;
import com.asiainfo.hybridapp.configs.ConstantsME;
import com.asiainfo.hybridapp.configs.EventBusTag;
import com.asiainfo.hybridapp.configs.InterfaceConfig;
import com.asiainfo.hybridapp.configs.RealInterfaceConfig;
import com.asiainfo.hybridapp.networkModel.HttpUtil;
import com.framework.utils.KeyBoardUtil;
import com.framework.utils.ScreenUtils;
import com.framework.utils.ToastUtil;
import com.framework.utils.Y;
import com.framework2.customLoading.LoadingIndicatorView;
import com.framework2.customviews.TitleView;
import com.framework2.utils.CustomProgressDialogUtils;
import com.framework2.utils.TBSCookieManagerUtil;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.CookieManager;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import custom.org.greenrobot.eventbus.Subscribe;
import custom.org.greenrobot.eventbus.ThreadMode;

/**
 * 通用加载TBS-webview的 LoginTBSWebViewActivity
 *
 * @author YobertJomi
 * className LoginTBSWebViewActivity
 * created at  2017/6/13  15:54
 */
public class LoginTBSWebViewActivity extends BaseActivity {

    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.progress)
    ProgressBar progress;
    @BindView(R.id.tvEmpty)
    TextView tvEmpty;
    @BindView(R.id.emptyLayout)
    LinearLayout emptyLayout;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.onLoading)
    LoadingIndicatorView onLoading;

    private boolean isShowingError;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_tbs_webview);
        ButterKnife.bind(this);
        ScreenUtils.getInstance().setTranslucentStatus(this, true);
        ScreenUtils.getInstance().setStatusBarTintColor(this,
                getResources().getColor(R.color.white));
        initView();
        TBSCookieManagerUtil.getInstance().synCookies(this, RealInterfaceConfig.getRealBaseServerUrl());
        initWebViewSetting();
        loadUrl(null == getIntent() ? null : getIntent().getStringExtra(ConstantsME.url));
    }

    //eventBus通知新消息
    @Subscribe(threadMode = ThreadMode.MAIN, tag = EventBusTag.newMessage)
    public void receivedNewMessage(String info) {
    }

    private void initView() {
        try {
//            titleView.setRightVisible(false).setLeftOnClickListener(new BaseOnClickListener() {
//                @Override
//                protected void onBaseClick(View v) {
//                    goBack();
//                }
//            });
            titleView.setTitle(getIntent().getStringExtra(ConstantsME.title));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initWebViewSetting() {

        WebSettings webSettings = webView.getSettings();
        //支持缩放，默认为true。
        webSettings.setSupportZoom(false);
        //调整图片至适合webview的大小
        webSettings.setUseWideViewPort(true);
        // 缩放至屏幕的大小
        webSettings.setLoadWithOverviewMode(true);
        //设置默认编码
        webSettings.setDefaultTextEncodingName("utf-8");
        ////设置自动加载图片
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setGeolocationEnabled(true);
        webSettings.setDatabaseEnabled(false);
        webSettings.setAppCacheEnabled(false);

//        webSettings.setDatabaseEnabled(true);
//        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
//        webSettings.setDatabasePath(dir);

        //多窗口
        webSettings.supportMultipleWindows();
        //获取触摸焦点
        webView.requestFocusFromTouch();
        //允许访问文件
        webSettings.setAllowFileAccess(true);
        //开启javascript
        webSettings.setJavaScriptEnabled(true);
        //支持通过JS打开新窗口
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        //提高渲染的优先级
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        //支持内容重新布局
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        //优先webview中使用缓存
//        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        //设置 缓存模式
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        webSettings.setDomStorageEnabled(true);
        webSettings.setBlockNetworkImage(true);
    }

    private void loadUrl(String url) {
        try {
            isShowingError = false;
            webView.setVisibility(View.VISIBLE);
            onLoading.setVisibility(View.VISIBLE);
            progress.setVisibility(View.VISIBLE);
            emptyLayout.setVisibility(View.GONE);
            webView.loadUrl(!TextUtils.isEmpty(url) ? url : "http://www.baidu.com");//http://www.baidu.com
            //如果不设置WebViewClient，请求会跳转系统浏览器
            webView.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    //该方法在Build.VERSION_CODES.LOLLIPOP以前有效，从Build.VERSION_CODES
                    // .LOLLIPOP起，建议使用shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead
                    //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
                    //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，我想让webView加载http://ask.csdn.net/questions/178242
                    Y.y("LoginWebViewActivity:" + url);

                    CookieManager cookieManager = CookieManager.getInstance();
                    String cookie =
                            cookieManager.getCookie(RealInterfaceConfig.getRealBaseServerUrl() + InterfaceConfig.jsCLickLogin);
                    if (!TextUtils.isEmpty(cookie) && cookie.contains(";")) {
                        Y.y("LoginWebViewActivity成功后保存的cookie：" + cookie);
                        cookieManager.removeAllCookie();
                        TBSCookieManagerUtil.getInstance().saveCookie(LoginTBSWebViewActivity.this, cookie);
                    }

                    if (TextUtils.equals(url, RealInterfaceConfig.getRealBaseServerUrl() + InterfaceConfig.webMain)
                            || TextUtils.equals(url,
                            RealInterfaceConfig.getRealBaseServerUrl() + InterfaceConfig.webKPIdetail)) {
                        TBSCookieManagerUtil.getInstance().synCookies(LoginTBSWebViewActivity.this,
                                RealInterfaceConfig.getRealBaseServerUrl());
                        startActivity(HomepageActivity.class);
                        finishActivity();
                        return true;
                    }
                    view.loadUrl(url);
                    return true;
                }

                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
                    //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，我想让webView加载http://ask.csdn.net/questions/178242
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                    }
                    String url = request.getUrl().toString();
                    Y.y("shouldOverrideUrlLoading222:" + url);

                    CookieManager cookieManager = CookieManager.getInstance();
                    String cookie =
                            cookieManager.getCookie(RealInterfaceConfig.getRealBaseServerUrl() + InterfaceConfig.jsCLickLogin);
                    Y.y("fragment222-shouldOverrideUrlLoading成功后保存的cookie：" + cookie);
                    if (!TextUtils.isEmpty(cookie) && cookie.contains(";")) {
                        cookieManager.removeAllCookie();
                        TBSCookieManagerUtil.getInstance().saveCookie(LoginTBSWebViewActivity.this, cookie);
                    }

                    if (TextUtils.equals(url, RealInterfaceConfig.getRealBaseServerUrl() + InterfaceConfig.webMain)
                            || TextUtils.equals(url,
                            RealInterfaceConfig.getRealBaseServerUrl() + InterfaceConfig.webKPIdetail)) {
                        TBSCookieManagerUtil.getInstance().synCookies(LoginTBSWebViewActivity.this,
                                RealInterfaceConfig.getRealBaseServerUrl());
                        startActivity(HomepageActivity.class);
                        return true;
                    }
                    view.loadUrl(url);
                    return true;
                }

//                @Override
//                public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
//                    super.onReceivedError(view, request, error);
////                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
////                        request.getUrl();
////                    }
//                    webViewTemp=view;
//                    isShowingError = true;
//                    webView.setVisibility(View.GONE);
//                    onLoading.setVisibility(View.GONE);
//                    progress.setVisibility(View.GONE);
//                    emptyLayout.setVisibility(View.VISIBLE);
//                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
//                    if (!webView.getSettings().getLoadsImagesAutomatically()) {
//                        webView.getSettings().setLoadsImagesAutomatically(true);
//                    }
                }
            });
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
//                    Y.y("newProgress:" + newProgress);
                    if (!isShowingError)
                        if (newProgress >= 100) {
                            webView.setVisibility(View.VISIBLE);
                            progress.setProgress(100);
                            onLoading.setVisibility(View.GONE);
                            progress.setVisibility(View.GONE);
                            emptyLayout.setVisibility(View.GONE);
                        } else {
                            webView.setVisibility(View.VISIBLE);
                            progress.setProgress(newProgress);
                            onLoading.setVisibility(View.GONE);// TODO: 2017/6/13 可以修改为visible
                            progress.setVisibility(View.VISIBLE);
                            emptyLayout.setVisibility(View.GONE);
                        }
                }
            });
        } catch (Exception e) {
            Y.y("loadUrl-Exception:" + e.getMessage());
            webView.setVisibility(View.GONE);
            onLoading.setVisibility(View.GONE);
            progress.setVisibility(View.GONE);
            emptyLayout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 请求更改昵称
     */
    private void requestModifyNick(@NonNull String nick) {
        HttpUtil.getInstance().requestModifyNick(InterfaceConfig.modifyNick, nick,
                new HttpUtil.OnRequestResult<String>() {
                    @Override
                    public void onSuccess(String... s) {
                        CustomProgressDialogUtils.dismissProcessDialog();
                        finishActivity();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        CustomProgressDialogUtils.dismissProcessDialog();
                        ToastUtil.getInstance().showToast(TextUtils.isEmpty(msg) ? "修改昵称失败" : msg);
                    }
                });
    }

    @OnClick(R.id.emptyLayout)
    public void onViewClicked() {
//        loadUrl();
    }

    @Override
    protected void onDestroy() {
        if (webView != null) {
            try {
                // 如果先调用destroy()方法，则会命中if (isDestroyed()) return;这一行代码，需要先onDetachedFromWindow()，再
                // destory()
                ViewParent parent = webView.getParent();
                if (parent != null) {
                    ((ViewGroup) parent).removeView(webView);
                }
                webView.stopLoading();
                // 退出时调用此方法，移除绑定的服务，否则某些特定系统会报错
                webView.getSettings().setJavaScriptEnabled(false);
                webView.clearHistory();
                webView.clearView();
                webView.removeAllViews();
                webView.destroy();
            } catch (Exception ex) {

            }
        }
        super.onDestroy();
    }

    private void goBack() {
        if (null != webView && webView.canGoBack() && !TextUtils.equals(webView.getUrl(),
                RealInterfaceConfig.getRealBaseServerUrl() + InterfaceConfig.webMain)) {
            webView.goBack();
        } else {
            KeyBoardUtil.getInstance().isCloseSoftInputMethod(this, null, true);
            beforeFinishActivity();
            finishActivity();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
